#include "mainwindow.h"
#include <QString>
#include <QDebug>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent){
    init();
    initGame();

}

MainWindow::~MainWindow(){
    delete  pipeChannels;
}
/**
 * 复写绘制函数
 * @brief paintEvent
 * @param event
 */
void MainWindow::paintEvent(QPaintEvent *){
        //qDebug() << "paintEvent";
      drawBack();
      drawPipe();
      drawBird();
}
/**
 * @brief keyPressEvent
 * @param event
 * 键盘按下事件
 */
void MainWindow::keyPressEvent(QKeyEvent *event){
    if (event->key() == Qt::Key_Space) {
        birdStatus = BirdStatus::UP;
        initSpeed();
    }
}
/**
  * @brief mousePressEvent
  * @param event
  * 鼠标点击事件
  */
 void MainWindow::mousePressEvent(QMouseEvent * ){
    // qDebug() << "click";
     birdStatus = BirdStatus::UP;
     initSpeed();
 }

/**
 * @brief MainWindow::init
 * 初始化
 */
void MainWindow::init(){
    this->move(400, 300);
    this->setFixedSize(480, 640);
     qDebug() << "init ";

     // D:/QtProjects/build-flappy_bird-Desktop_Qt_5_13_0_MinGW_64_bit-Release/release
   qDebug() << QCoreApplication::applicationDirPath()<<endl; // 获取程序运行时的目录，将资源文件夹复制到该目录

   ground = QPixmap( QCoreApplication::applicationDirPath().append(R"(/res/ground.png)")); // 使用绝对路径
    if(!ground){
        qDebug() << "ground is null" << endl;
    }
    ground = ground.scaled(20,80); // 缩放
    int groundHeight = ground.height();
    background = QPixmap( QCoreApplication::applicationDirPath().append(R"(/res/background.png)"));
    background = background.scaled(this->width(), this->height() - groundHeight); //缩放

    groundSize = this->width()/ground.width() + 1; //防止除不尽有余数，所以加1
    v = new QVector<int>(groundSize);

    for(int i = 0; i < groundSize; i++){
         int x = i * ground.width();
         (*v)[i] = x;

    }
    QPixmap bird( QCoreApplication::applicationDirPath().append(R"(/res/bird.png)"));
    bird1 = bird.copy(0, 0, 92, 64);
    bird1 = bird1.scaled(60, 42);

    bird2 = bird.copy(92, 0, 92, 64);
    bird2 = bird2.scaled(60, 42);

    bird3 = bird.copy(92*2, 0, 92, 64);
    bird3 = bird3.scaled(60, 42);

    scoreLabel = new QLabel(this);
    scoreLabel->setGeometry(width()/2, 0, 50, 50);

    QFont font ( "Microsoft YaHei", 20, 75); //第一个属性是字体（微软雅黑），第二个是大小，第三个是加粗（权重是75）
    scoreLabel->setFont(font);
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::white);
    scoreLabel->setPalette(pa);
    this->score = 0;
    scoreLabel->setText(QString::number(this->score));

    startButton = new QPushButton(this);
    font = QFont( "Microsoft YaHei", 25, 75); //第一个属性是字体（微软雅黑），第二个是大小，第三个是加粗（权重是75）
    startButton->setFont(font);
    startButton->setPalette(pa);
    startButton->setText(QString("start"));
    startButton->setGeometry(width()/2 -50, height()/2 -30, 100, 60);
   // startButton->hide();

    timer = new QTimer(this);
    //事件
    connect(timer,SIGNAL(timeout()),this,SLOT(loopPaint()));
    connect(startButton,SIGNAL(clicked()),this,SLOT(slotStartGame()));
}
/**
 * @brief initGame
 * 初始化游戏相关
 */
void MainWindow::initGame(){

    birdX = this->width() / 3;
    birdY = this->height() / 2 - bird1.height();
    this->score = 0;
    scoreLabel->setText(QString::number(this->score));
    uint seed_x = static_cast<uint>(clock());
    int l = GlobalUtils::getRandomNum(seed_x,200); //获取随机数
    if(pipeChannels == nullptr){
        pipeChannels = new QVector<PipeChannel*>(2); //创建两个通道
    }else {
        delete (*pipeChannels)[0];
        delete (*pipeChannels)[1];
    }
    (*pipeChannels)[0] = new PipeChannel(height(), 100+l, width()+100, ground.height(), this);
    (*pipeChannels)[1] = new PipeChannel(height(), 150 +l, 2 * width() + l, ground.height(), this);
}

 void MainWindow::initSpeed(){
     birdDownSpeed = 2.0;
     birdUpSpeed = 5.0;
 }
/**
 * @brief startGame
 * 开始游戏
 */
void MainWindow::startGame(){
    startButton->hide();
    initGame();
    initSpeed();
    gameStatus = GameStatus::RUNNING;
    birdStatus = BirdStatus::DOWN;
    timer->start(16);
}
/**
 * @brief stopGame
 * 停止游戏
 */
void MainWindow::stopGame(){
    timer->stop();
    gameStatus = GameStatus::STOPING;
    startButton->show();
}
/**
 * @brief drawBack
 * 绘制背景
 */
void MainWindow::drawBack(){
     QPainter painter(this);
     int height = ground.height();
     int pos;
     // 绘制背景图
     painter.drawPixmap(0,0, background);
     // 绘制地面
     for(int i = 0; i < groundSize; i++){
         pos = (*v)[i];
         painter.drawPixmap(pos,this->height() - height, ground);//绘制一个图像
         // 改变坐标 移动起来
         pos -= moveSpeed;
         if(pos <= -ground.width()){
             pos = (groundSize-1) *  ground.width();
         }
         (*v)[i] = pos;
     }

}
/**
 * @brief drawBack
 * 绘制bird
 */
void MainWindow::drawBird(){

     QPainter painter(this);
     QPixmap bird = bird1;
     if(gameStatus == GameStatus::RUNNING){
         //绘制哪一个 (动画效果)
         if(birdNo < birdNoMax/3){
             bird = bird1;
         }else if(birdNo < birdNoMax/3 * 2){
             bird = bird2;
         }else{
             bird = bird3;
         }
         birdNo++;
         if(birdNo > birdNoMax){
             birdNo = 1;
         }
     }
     // 小鸟下降
     if(birdStatus == BirdStatus::DOWN){
         birdY += birdDownSpeed;
         birdDownSpeed+=0.1;
        // matrix.rotate(1); //下降的同时旋转
        // bird = bird.transformed(matrix, Qt::SmoothTransformation);
     }
    // 判断是否碰撞
     if(isCrush()){
        stopGame();
     }
    // 上升
     if(birdStatus == BirdStatus::UP){
         birdY -= birdUpSpeed;
         birdUpSpeed -= 0.2;
        // matrix.rotate(-1);
        // bird = bird.transformed(matrix, Qt::SmoothTransformation);
         if(birdUpSpeed <= 0.0){
             birdStatus = BirdStatus::DOWN;
            // qDebug() << "down";
             initSpeed();
         }
     }


     painter.drawPixmap(birdX, birdY, bird);


}
/**
 * @brief MainWindow::drawPipe
 * 绘制管道
 */
void MainWindow::drawPipe(){
    QPainter painter(this);
    uint seed_x = static_cast<uint>(clock());
    int l = GlobalUtils::getRandomNum(seed_x,200); //获取随机数
    int otherX = 0; //另一个通道的位置
    for(int i = 0; i < pipeChannels->size(); i++){
        PipeChannel * pipeChannel = (*pipeChannels)[i];
        if(i ==0){
            otherX = (*pipeChannels)[1]->getX();
        }else {
            otherX = (*pipeChannels)[0]->getX();
        }
        pipeChannel->draw(painter);
        int x = pipeChannel->getX();

        if(x + pipeChannel->getPipeWidth() < birdX && !pipeChannel->isScore){
            this->score++; //增加得分
            scoreLabel->setText(QString::number(this->score));
            pipeChannel->isScore = true;
        }
        x -= moveSpeed; // 改变坐标移动起来
        if(x <= -pipeChannel->getPipeWidth()){ // 移出窗口了
            x = otherX + width() / 2 +  2 * l; //重新设置位置
            pipeChannel->setH1(50+l); //重新设置高度
            pipeChannel->initPipe();//重新初始化
        }
        pipeChannel->setX(x);

    }

}
/**
 * @brief isCrush 是否碰撞
 * @return
 */
bool MainWindow::isCrush(){
    if (birdY >= this->height() - ground.height() - bird1.height()){ //是否碰撞地面
        return true;
    }
    //是否碰撞管道
    for(PipeChannel* c : *pipeChannels){
        if(birdX + bird1.width() > c->getX() && birdX < c->getX() + c->getPipeWidth()
                && (birdY < c->getPiPe2Height() || birdY+bird1.height() > c->getY1())){
            return true;
        }

    }
    return false;
}
void MainWindow::loopPaint(){
    update();
}
void MainWindow::slotStartGame(){
    startGame();
}

